package com.wtp.wallpaper.pojo.query;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.Data;

@Data
public class PageQuery {
    private Integer pageNum = 1;
    private Integer pageSize = 5;
    //排序字段
    private String sortBy;
    //是否升序
    private Boolean isAsc = true;

    public PageQuery(){}

    public PageQuery(Integer pageNum,Integer pageSize){
        this.pageNum = pageNum;
        this.pageSize = pageSize;
    }

    public <T> Page<T> toMpPage (OrderItem... items){//构建page分页条件
        Page<T> page = Page.of(pageNum,pageSize);
        if(StrUtil.isNotBlank(sortBy)){
            page.addOrder(isAsc ? OrderItem.asc(sortBy) : OrderItem.desc(sortBy));
        }else if(items != null){
            page.addOrder(items);
        }
        return page;
    }

    public <T> Page<T> toMpPage(String defaultSortBy,Boolean defaultAsc){
        return toMpPage(defaultAsc ? OrderItem.asc(defaultSortBy) : OrderItem.desc(defaultSortBy));
    }
    public <T> Page<T> toMpPageDefaultSortByCreateTime(){
        return toMpPage(OrderItem.desc("create_time"));
    }
    public <T> Page<T> toMpPageDefaultSortByUpdateTime(){
        return toMpPage(OrderItem.desc("update_time"));
    }

}
